package GUI.AddBooking;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import GUI.Controller.Controller;
import GUI.TabPanels.BookingPanel;

@SuppressWarnings("serial")
public class ViewAvailableRoomsPanel extends JPanel{
	private BookingPanel booking_panel;
	private JTable bookingTable;

	private JPanel north_panel;
	private JScrollPane scrollPane;

	private JButton cancel;
	private JButton book;

	String[] columnNames ={
			"Room ID", "Room #", "Room Type", "Hotel Name", "Room Cost"};

	public ViewAvailableRoomsPanel(){
		booking_panel = BookingPanel.getInstance();
		this.setLayout(new BorderLayout());
		this.setVisible(true);
		SetUpNorthPanel();
		SetUpTable();
	}

	private void SetUpNorthPanel(){

		north_panel = new JPanel();
		north_panel.setLayout(new BorderLayout());
		this.add(north_panel, BorderLayout.NORTH);

		book = new JButton("Book Room");
		north_panel.add(book, BorderLayout.WEST);
		book.addActionListener(new BookRoomButton());

		cancel = new JButton("Cancel");
		north_panel.add(cancel, BorderLayout.EAST);
		cancel.addActionListener(new CancelButton());

	}

	private void SetUpTable(){
		Object[][] tableData;
		try{
			tableData= Controller.GetAvailableRooms(booking_panel.getAvailableRoomsInfo());
		}catch(NullPointerException e){
			tableData = null;
		}

		bookingTable = new JTable(tableData, columnNames);

		bookingTable.setModel(new DefaultTableModel(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false; 
			} 
		});
		bookingTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		bookingTable.setPreferredScrollableViewportSize(new Dimension(500, 455));
		bookingTable.setFillsViewportHeight(true);

		if(scrollPane!=null)
			remove(scrollPane);
		scrollPane = new JScrollPane(bookingTable);
		this.add(scrollPane, BorderLayout.SOUTH);
	}
	
	public void Update(){
		SetUpTable();
		this.setVisible(false);
		this.setVisible(true);
	}

	class CancelButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Update();
			booking_panel.AddBookings();
		}
	}
	private class BookRoomButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// input[0] -> customerID
			// input[1] -> roomID
			// input[2] -> start_date
			// input[3] -> end_date
			if(bookingTable.getSelectedRow() == -1){
				JOptionPane.showMessageDialog(booking_panel, "Please select a room",
					    "Error",
					    JOptionPane.WARNING_MESSAGE);
			}
			String[] query = {null, null, null, null};
			String[] dates = booking_panel.getBookingDates();

			int row_index = bookingTable.getSelectedRow();
			
			int ID = Controller.GetCustomerID(booking_panel.getCustomerID()[0], booking_panel.getCustomerID()[1]);

			query[0] = Integer.toString(ID);
			query[1] = (String) bookingTable.getValueAt(row_index, 0);


			query[2] = dates[0];
			query[3] = dates[1];

			try{
				if(Controller.bookRoom(query)){
					Update();
					JOptionPane.showMessageDialog(null, "Room Booked!");
					booking_panel.ViewBookings();
				}
				else
					JOptionPane.showMessageDialog(null, "Invalid Room Choice!");

			}
			catch(Exception e){
				JOptionPane.showMessageDialog(null, "Invalid Room Choice!");
			}
		}
	}
}
